#include <REGX52.H>
#include "Key.h"
#include "DS1302.h"
#include "LCD1602.h"
#include "Delay.h"
#include "Timer0.h"
#include "Buzzer.h"


sbit Buzzer = P2^5;

//分别用于模式选择，接收独立按键返回值，调整数组，控制闪烁
unsigned char MODE,KeyNum,TimeNumSelect,TimeSetFlashFlag;


void TimeShow()//时钟显示
{
    DS1302_ReadTime();//读取时间

    LCD_ShowNum(1,1,DS1302_Time[0],2);//年
    LCD_ShowNum(1,4,DS1302_Time[1],2);//月
    LCD_ShowNum(1,7,DS1302_Time[2],2);//日
    LCD_ShowNum(2,1,DS1302_Time[3],2);//时
    LCD_ShowNum(2,4,DS1302_Time[4],2);//分
    LCD_ShowNum(2,7,DS1302_Time[5],2);//秒
}

void TimeSet()//时钟设置
{
    if (2 == KeyNum)
    {
        TimeNumSelect++;
        TimeNumSelect %= 6;//越界清零
    }
    if (3 == KeyNum)//加
    {
        DS1302_Time[TimeNumSelect]++;
    /*************************年*********/
        if (DS1302_Time[0] > 99){ DS1302_Time[0] = 0; }
    /************************月*********/
        if (DS1302_Time[1] > 12){ DS1302_Time[1] = 1; }
    /************************日*********/
        if (DS1302_Time[2] > 30 && (DS1302_Time[1] == 4 || DS1302_Time[1] == 6 || DS1302_Time[1] == 9 || DS1302_Time[1] == 11))
        { DS1302_Time[2] = 1; }
        if (DS1302_Time[2] > 31 && (DS1302_Time[1] == 1 || DS1302_Time[1] == 3 || DS1302_Time[1] == 5 || DS1302_Time[1] == 7 || DS1302_Time[1] == 8 || DS1302_Time[1] == 10 || DS1302_Time[1] == 12))
        { DS1302_Time[2] = 1; }
        if (DS1302_Time[2] > 29 && DS1302_Time[1] == 2 && ((DS1302_Time[0]%4 ==0 && DS1302_Time[0]%100 != 0) || DS1302_Time[0]%400 == 0))
        { DS1302_Time[2] = 1; }	
        if (DS1302_Time[2] > 28 && DS1302_Time[1] == 2 && ((DS1302_Time[0]%4 !=0 && DS1302_Time[0]%100 == 0) || DS1302_Time[0]%400 != 0))
        { DS1302_Time[2] = 1; }	
    /************************时*********/
        if (DS1302_Time[3] >= 24){ DS1302_Time[3] = 0; }
    /************************分*********/
        if (DS1302_Time[4] >= 60){ DS1302_Time[4] = 0; }
    /************************秒*********/
        if (DS1302_Time[5] >= 60){ DS1302_Time[5] = 0; }
        
    }
    if (4 == KeyNum)//减,255是因为之前定义的unsigned char范围是0~255
    {
        DS1302_Time[TimeNumSelect]--;
    /*************************年*********/
        if (DS1302_Time[0] == 255){ DS1302_Time[0] = 99; }
    /************************月*********/
        if (DS1302_Time[1] == 255){ DS1302_Time[1] = 12; }
    /************************日*********/
        if (DS1302_Time[2] < 1 && (DS1302_Time[1] == 4 || DS1302_Time[1] == 6 || DS1302_Time[1] == 9 || DS1302_Time[1] == 11))
        { DS1302_Time[2] = 30; }
        if (DS1302_Time[2] < 1 && (DS1302_Time[1] == 1 || DS1302_Time[1] == 3 || DS1302_Time[1] == 5 || DS1302_Time[1] == 7 || DS1302_Time[1] == 8 || DS1302_Time[1] == 10 || DS1302_Time[1] == 12))
        { DS1302_Time[2] = 31; }
        if (DS1302_Time[2] < 1 && DS1302_Time[1] == 2 && ((DS1302_Time[0]%4 ==0 && DS1302_Time[0]%100 != 0) || DS1302_Time[0]%400 == 0))
        { DS1302_Time[2] = 29; }
        if (DS1302_Time[2] < 1 && DS1302_Time[1] == 2 && ((DS1302_Time[0]%4 !=0 && DS1302_Time[0]%100 == 0) || DS1302_Time[0]%400 != 0))
        { DS1302_Time[2] = 28; }
    /************************时*********/
        if (DS1302_Time[3] == 255){ DS1302_Time[3] = 23; }
    /************************分*********/
        if (DS1302_Time[4] == 255){ DS1302_Time[4] = 59; }
    /************************秒*********/
        if (DS1302_Time[5] == 255){ DS1302_Time[5] = 59; }
    }
    /********************更新显示并控制闪烁******************************/
    if (0 == TimeNumSelect && 1 == TimeSetFlashFlag)//年闪烁
        LCD_ShowString(1,1,"  ");
    else 
        LCD_ShowNum(1,1,DS1302_Time[0],2);//年
    if (1 == TimeNumSelect && 1 == TimeSetFlashFlag)//月闪烁
        LCD_ShowString(1,4,"  ");
    else 
        LCD_ShowNum(1,4,DS1302_Time[1],2);//月
    if (2 == TimeNumSelect && 1 == TimeSetFlashFlag)//日闪烁
        LCD_ShowString(1,7,"  ");
    else
        LCD_ShowNum(1,7,DS1302_Time[2],2);//日
    if (3 == TimeNumSelect && 1 == TimeSetFlashFlag)//时闪烁
        LCD_ShowString(2,1,"  ");
    else
        LCD_ShowNum(2,1,DS1302_Time[3],2);//时
    if (4 == TimeNumSelect && 1 == TimeSetFlashFlag)//分闪烁
        LCD_ShowString(2,4,"  ");
    else
        LCD_ShowNum(2,4,DS1302_Time[4],2);//分
    if (5 == TimeNumSelect && 1 == TimeSetFlashFlag)//秒闪烁
        LCD_ShowString(2,7,"  ");
    else
        LCD_ShowNum(2,7,DS1302_Time[5],2);//秒
}

void main()
{
    Timer0_Init();
    LCD_Init();
    DS1302_Init();
    DS1302_SetTime();

    LCD_ShowString(1,1,"  -  -  ");//显示基本框架
    LCD_ShowString(2,1,"  :  :  ");

    while(1)
    {
        KeyNum = Key();
        if (KeyNum)
        {
            Buzzer_Time(50);
        }
        if (1 == KeyNum)
        {
            if (0 == MODE)
            {MODE = 1;TimeNumSelect = 0;}
            else if (1 == MODE)
            { MODE = 0;DS1302_SetTime(); }
        }
        switch (MODE)
        {
            case 0:TimeShow();break;
            case 1:TimeSet();break;
        }
    }
}


void Timer0_Rountine() interrupt 1
{
    static unsigned int T0Count = 0;

    TL0 = 0x66;     //设置定时初值
    TH0 = 0xFC;     //设置定时初值
    T0Count++;
    if (T0Count >= 500)
    {
        T0Count = 0;
        TimeSetFlashFlag = !TimeSetFlashFlag;//逻辑取反
    }
}

























